// promise.all的规则
// 1. promise都是fulfilled，返回由它们的值组成，状态为fulfilled的全新的promise
// 2. 只要有一个promise为rejected，则返回rejected状态新的promise，且它的值是第一个rejected的promise值
// 3. 只要有一个promise为pending，则返回一个pending状态的新的Promise
function promiseAll(promiseArr){
    let result = [],index = 0
    return new Promise((resovle,reject) => {
        promiseArr.forEach((p,i) => {
            Promise.resolve(p).then(val => {
                result[i] = val
                index++
                if(index == promiseArr.length - 1) {
                    resovle(result)
                }
            },
            err => {
                reject(err)
            })
        })
    })
}

let p1 = new Promise((resolve,reject) => {
    resolve('ok')
})
let p2 = new Promise((resolve,reject) => {
    resolve('ok')
})
let p3 = new Promise((resolve,reject) => {
    resolve('ok')
})

promiseAll([p1,p2,p3]).then(res => {
    console.log("sucess:",res);
},err=> {
    console.log("err:",err);
})